﻿using System;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Framework.Serialization;
using IQIGame.Onigao.GamePlay;
using LFloatMath.Math;

namespace IQIGame.Onigao.Logic
{
    public class SkillEffective_ExtraDamage : SkillEffective
    {
        protected override ISerializeCustomPOD OnExecute(SkillEffectiveData data, Unit target, SkillEffectiveContext context, int effectiveIndex, out SkillEffectiveResponse startResp)
        {
            startResp = null;
            if (data.effectiveConfig.customConfig is SkillEffective_ExtraDamage_ConfigData rConfig && context.param is DamageResponse rOldResp)
            {
                var rAttacker = target.battle.playCtrl.unitCtrl.GetUnit(rOldResp.attacker);
                var nExtraDamage = Formula.Calculate<LFloat, int>(rConfig.damageFormula, rAttacker, target, rOldResp.finalDamage).ToInt();
                var rDamageResp = DamageController.TakeDamageWithoutShield(rAttacker, target, EDamageFrom.Skill,
                    (EDamageType)(1 << context.skill.config.DamageType), nExtraDamage, context.skill, rConfig.checkDead);
                return rDamageResp;
            }

            return null;
        }

#if UNITY_EDITOR
        public override string ToString(SkillCustomConfig config)
        {
            var rConfig = config as SkillEffective_ExtraDamage_ConfigData;
            return $"根据公式{rConfig.damageFormula}额外伤害";
        }
#endif
    }
}